/*******************************************************************************
* @copyright: Shenzhen Hangshun Chip Technology R&D Co., Ltd
* @filename:  main.c
* @brief:     Main program body
* @author:    AE Team
* @version:   V1.0.0/2025-07-16
*             1.Initial version
* @log:
*******************************************************************************/


/* Includes ------------------------------------------------------------------*/
#include "main.h"


/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
__IO FLASH_Status FLASHStatus = FLASH_COMPLETE;

/* Private function prototypes -----------------------------------------------*/
void ResetPinToPA0_Configuration(void);
static void Delay(__IO uint32_t nCount);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (KEIL_startup_hk32g003.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_hk32g003.c file
     */
  Delay(0xFFFFF);

  ResetPinToPA0_Configuration();

  while(1)
  {
    GPIOA->ODR ^= 0x01;
    Delay(0xFF);
  }
}

/**
  * @brief  Configure Reset pin to PA0 Function.
  * @param  None
  * @retval None
  */
void ResetPinToPA0_Configuration(void)
{
  uint32_t OB_Value;

  GPIO_InitTypeDef GPIO_InitStructure;

  /* Enable IOMUX clock */
  RCC_APBPeriph1ClockCmd(RCC_APBPeriph1_IOMUX, ENABLE);

  RCC_APBPeriph2ClockCmd(RCC_APBPeriph2_SYSCFG, ENABLE);

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  /* Unlock the Flash Program Erase controller */
  FLASH_Unlock();

  FLASH_OB_Unlock();

  OB_Value = *(__IO uint32_t*)0x1FFFF018;

  if( (OB_Value & 0xFFFF0000) != 0x34560000)
  {
    /* Erase the WRP option Bytes*/
    FLASHStatus = FLASH_OB_Erase();

    OB_Value &= 0x0000FFFF;
    OB_Value |= 0x3450000;

    FLASH_OB_ProgramData(0x1FFFF018, 0x34560000);
  }

  FLASH_OB_Lock();

  FLASH_Lock();

  GPIO_IOMUX_ChangePin(IOMUX_PIN4, IOMUX_NRST_SEL_PA0);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}

/**
  * @brief  Inserts delay time.
  * @param  nCount: specifies the delay time length.
  * @retval None
  */
static void Delay(__IO uint32_t nCount)
{
  /* Decrement nCount value */
  while (nCount != 0)
  {
    nCount--;
  }
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif


